<?php
/**
 * Created by PhpStorm.
 * Login: Administrator
 * Date: 2017-11-14
 * Time: 09:14
 */
namespace app\index\controller;
use think\Controller;
use phpmailer\phpmailer;
//use app\admin\model\Login;
use think\Db;

class Misspass extends Controller
{
    /*public function savepass()
    {//修改密码
        if (isset($_POST['submit']) && $_POST['submit'] == "确定修改") {
            $email = input('post.email');
            $ch_name = input('post.ch_name');
            $rand = input('post.rand');
            $password = input('post.password');
            $result1 = Db::table('oa_user')->where(['ch_name' => $ch_name, 'email' => $email])->select();
            $result2 = Db::table('oa_user')->where(['en_name' => $ch_name, 'email' => $email])->select();
            $results = Db::table('oa_rand_email')->where(['rand' => $rand, 'user_email' => $email])->select();
            if (!empty($result1) && !empty($results)) {
                if (!empty($password)) {
                    $pass = md5($password);
                    $where['password'] = $pass;
                    $num = Db::table('oa_user')->where(['id' => $result1[0]['id']])->update($where);
                    if ($num) {
                        return $num;
                    } else {
                        return 0;
                    }
                }
            } elseif (!empty($result2) && !empty($results)) {
                if (!empty($password)) {
                    $pass = md5($password);
                    $where['password'] = $pass;
                    $num = Db::table('oa_user')->where(['id' => $result2[0]['id']])->update($where);
                    if ($num) {
                        return $num;
                    } else {
                        return 0;
                    }
                }
            } else {
                return 0;
            }
        }
        return $this->fetch();
    }*/

    public function savepass()
    {//修改密码
        if (isset($_POST['sub']) && $_POST['sub'] == "确定修改") {
            $res = input('post.data');
            $result = json_decode($res, true);
            $email = $result['email'];
            $ch_name = $result['ch_name'];
            $rand = $result['rand'];
            $password = $result['password'];
            $result1 = Db::table('oa_user')->where(['ch_name' => $ch_name, 'email' => $email])->select();
            $result2 = Db::table('oa_user')->where(['en_name' => $ch_name, 'email' => $email])->select();
            $results = Db::table('oa_rand_email')->where(['rand' => $rand, 'user_email' => $email])->select();
            if (count($result1) == 1 && count($results) == 1) {
                if (!empty($password)) {
                    $pass = md5($password);
                    $where['password'] = $pass;
                    //验证与修改前密码是否一致
                    $password_equal1 = Db::table('oa_user')->where(['id' => $result1[0]['id'], 'password' => $pass])
                        ->select();
                    if (!empty($password_equal1)) {
                        return 2;
                    } else {
                        $num = Db::table('oa_user')->where(['id' => $result1[0]['id']])->update($where);
                        if ($num) {
                            return $num;
                        } else {
                            return 0;
                        }
                    }
                }
            } elseif (count($result2) == 1 && count($results) == 1) {
                if (!empty($password)) {
                    $pass = md5($password);
                    $where['password'] = $pass;
                    $password_equal2 = Db::table('oa_user')->where(['id' => $result2[0]['id'], 'password' => $pass])
                        ->select();
                    if (!empty($password_equal2)) {
                        return 2;
                    } else {
                        $num = Db::table('oa_user')->where(['id' => $result2[0]['id']])->update($where);
                        if ($num) {
                            return $num;
                        } else {
                            return 0;
                        }
                    }
                }
            } else {
                return 0;
            }
        }
        return $this->fetch();
    }

    public function getEmaila($str)
    {
        //验证邮箱(后台)
        $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
        if (preg_match($pattern, $str)) {
            return 1;
        } else {
            return 0;
        }
    }
    public function email_pass($email, $rand, $ch_name)
    {
        //发送邮件
        //$rand='sda';
        //$ch_name='asd';
        //$email = 'caowang@nobaogroup.com';//定义收件人的邮箱
        $mail = new PHPMailer();
        $mail->isSMTP();// 使用SMTP服务
        $mail->CharSet = "utf8";// 编码格式为utf8，不设置编码的话，中文会出现乱码
        $mail->Host = "smtp.163.com";// 发送方的SMTP服务器地址
        $mail->SMTPAuth = true;// 是否使用身份验证
        $mail->Username = "cw1993teemo@163.com";// 发送方的QQ邮箱用户名，就是自己的邮箱名
        $mail->Password = "asd18354682311";// 发送方的邮箱密码，不是登录密码,是qq的第三方授权登录码,要自己去开启,在邮箱的设置->账户->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 里面
        $mail->SMTPSecure = "ssl";// 使用ssl协议方式,
        $mail->Port = 465;// QQ邮箱的ssl协议方式端口号是465/587

        $mail->setFrom("cw1993teemo@163.com", $ch_name);// 设置发件人信息，如邮件格式说明中的发件人,
        $mail->addAddress($email, $ch_name);// 设置收件人信息，如邮件格式说明中的收件人
        $mail->addReplyTo("cw1993teemo@163.com", "Reply");// 设置回复人信息，指的是收件人收到邮件后，如果要回复，回复邮件将发送到的邮箱地址
        //$mail->addCC("xxx@163.com");// 设置邮件抄送人，可以只写地址，上述的设置也可以只写地址(这个人也能收到邮件)
        //$mail->addBCC("xxx@163.com");// 设置秘密抄送人(这个人也能收到邮件)
        //$mail->addAttachment("bug0.jpg");// 添加附件

        $mail->Subject = "忘记密码";
        //$mail->Subject = "找回密码";// 邮件标题
        $mail->Body = '你好：你的验证码为' . $rand . '，5分钟之内有效，若验证码错误，5分钟之后可重新获取';// 邮件正文
        $mail->AltBody = "This is the plain text纯文本";// 这个是设置纯文本方式显示的正文内容，如果不支持Html方式，就会用到这个，基本无用
        //return 1;
        if (!$mail->send()) {// 发送邮件
            return 0;
            //echo "Message could not be sent.";
            //echo "Mailer Error: ".$mail->ErrorInfo;// 输出错误信息
        } else {
            return 1;
            /*echo '发送成功';*/
        }

    }
    public function get_rand()
    {//生成验证码并发送至邮箱
        if (isset($_POST['submit_a']) && $_POST['submit_a'] == "点击获取验证码") {
            $email = input('post.email_b');
            $ch_name = input('post.ch_name');
            $yan_email = $this->getEmaila($email);
            if ($yan_email) {
                $result = Db::table('oa_user')->where(["ch_name" => $ch_name, "email" => $email])->select();
                $results = Db::table('oa_user')->where(["en_name" => $ch_name, "email" => $email])->select();
                if (count($result) == 1 || count($results) == 1) {
                    $time = time();
                    $num = Db::table('oa_rand_email')->field('start_time,end_time')
                        ->where(['user_email' => $email, 'start_time' => ['<', $time], 'end_time' => ['>', $time]])->select();
                    if (empty($num)) {
                        $rand = rand(1000, 9999);
                        //$send_email= new Login();
                        $res = $this->email_pass($email, $rand, $ch_name);
                        //return $res;
                        if ($res) {
                            $start_time = time();
                            $end_time = time() + 300;
                            $data['rand'] = $rand;
                            $data['start_time'] = $start_time;
                            $data['end_time'] = $end_time;
                            $data['user_email'] = $email;
                            $num = Db::table('oa_rand_email')->insert($data);
                            if ($num) {
                                return 1;
                            } else {
                                return 0;
                            }
                        } else {
                            return 0;
                        }
                    } else {
                        return 2;
                    }
                } else {
                    return 0;
                }
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

    public function verify_rand()
    {//验证验证码
        if (isset($_POST['submit_rand']) && $_POST['submit_rand'] == "确定") {
            $email_a = input('post.user_email');
            $ch_names = input('post.ch_name');
            $rand_a = input('post.rand');
            $starting_time = time();
            $yz_email = $this->getEmaila($email_a);
            if ($yz_email) {
                $str = Db::table('oa_user')->where(['ch_name' => $ch_names, 'email' => $email_a])->select();//验证用户名
                $str1 = Db::table('oa_user')->where(['en_name' => $ch_names, 'email' => $email_a])->select();//验证用户名拼音
                if (!empty($str) || !empty($str1)) {
                    $resultt = Db::table('oa_rand_email')->field('start_time,end_time')
                        ->where(['user_email' => $email_a, 'rand' => $rand_a, 'start_time' => ['<', $starting_time], 'end_time' => ['>', $starting_time]])->select();
                    if (!empty($resultt)) {
                        return 1;
                    } else {
                        return 0;
                    }
                } else {
                    return 0;
                }
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }
}